package littlewhisker.communication {
	import littlewhisker.globals.constants.errortypes.LWErrorTypes;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	/**
	 * @author Philip
	 */
	public class DelayedCall {
		
		private static var instance : DelayedCall;
		private static var allowInstantiation : Boolean;

		public function DelayedCall() {
			if (!allowInstantiation) {
            	throw new Error(LWErrorTypes.SINGLETON_INSTANSIATION);
			}
		}
	
      public static function getInstance():DelayedCall {
         if (instance == null) {
            allowInstantiation = true;
            instance = new DelayedCall();
            allowInstantiation = false;
          }
         return instance;
       }

		public function makeInt(func:Function, delayMs:int):void {
			var nTimer : Timer = new Timer(delayMs, 1);
			nTimer.addEventListener(TimerEvent.TIMER_COMPLETE, func);
			nTimer.start();
		}

		public static function make(func:Function, delayMs:int, ...callbackArguments):void {
			var tempFunc:Function;
			tempFunc = function():void{
				func.apply(false,callbackArguments);
			} 
			getInstance().makeInt(tempFunc,delayMs);			
		}

	}
}
